﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PcProject.Application.Service.Products;
using MiniPc.API.log4net;
using PcProject.Domain.Models.Entities;
using Microsoft.AspNetCore.Authorization;

namespace MiniPc.API.Controllers
{
    /// <summary>
    /// 商品分类控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize] //授权过滤
    public class ProCateController : ControllerBase
    {
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        private IProCateService _proCateService;
        /// <summary>
        /// 函数依赖注入
        /// </summary>
        /// <param name="proCateService"></param>
        public ProCateController(IProCateService proCateService)
        {
            _proCateService = proCateService;
        }


        /// <summary>
        /// 商品分类显示查询分页
        /// </summary>
        
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(/*int pageIndex = 1, int pageSize = 3*/)
        {
            try
            {
                //int totalCount;
                List<ProductCategory> lst = _proCateService.GetProCatesAsync(/*pageIndex, pageSize, out totalCount*/);
                return Ok(new { data = lst, code = 0, msg = ""/*, count = totalCount*/ });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// 树形（递归）
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TreeForm(int parentId = 0)
        {
            List<ProductCategory> menu = _proCateService.GetProCatesAsync();
            List<ProductCategory> menus = menu.Where(x => x.ParentId.Equals(parentId)).ToList();
            //实例化从元字典
            List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
            foreach (var item in menus)
            {
                Dictionary<string, object> json = new Dictionary<string, object>();
                json.Add("id", item.CateId);
                json.Add("title", item.CateName);
                json.Add("spread", item.ParentId);
                TreeForms(menu, json, item.CateId);
                data.Add(json);
            }
            return Ok(data);
        }

        /// <summary>
        /// 左侧树形子类
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="json"></param>
        /// <param name="id"></param>
        private void TreeForms(List<ProductCategory> menu, Dictionary<string, object> json, int id)
        {
            List<ProductCategory> menus = menu.Where(x => x.ParentId.Equals(id)).ToList();
            //实例化从元字典
            List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
            if (menus.Count == 0)
            {
                json.Add("children", null);
                return;
            }
            foreach (var item in menus)
            {
                Dictionary<string, object> jsons = new Dictionary<string, object>();
                jsons.Add("id", item.CateId);
                jsons.Add("title", item.CateName);
                jsons.Add("spread", item.ParentId);
                TreeForms(menu, jsons, item.CateId);
                data.Add(jsons);
            }
            json.Add("children", data);
        }

        /// <summary>
        /// 添加商品分类
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddProCate([FromForm] ProductCategoryDto obj)
        {
            obj.AddTimes = DateTime.Now;
            try
            {
                int n = await _proCateService.InsertAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除商品分类
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DelProCate(int id)
        {
            try
            {
                int n = await _proCateService.DeleAsync(id);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 反填商品分类
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Fill(int id)
        {
            try
            {
                Log4netHelper.Info($"用户进行操作");
                ProductCategory n = await _proCateService.FillAsync(id);
                return Ok(new { data = n, code = 1 });
            }
            catch (Exception ex)
            {
                Log4netHelper.Debug(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 修改商品分类
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UptProCate([FromForm] ProductCategoryDto obj)
        {
            try
            {
                int n = await _proCateService.UptAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

    }
}
